﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Net;

using EHTWebsite.Code;
using EHTWebsiteProject.Code.BLL;
using System.Resources;
using System.Threading;
using System.Globalization;
using System.Text;

namespace EHTWebsiteProject.Administrator
{
    public partial class EHTModul : System.Web.UI.Page
    {
        public Boolean IsVN = true;
        private String languageCode = "vi-VN";
        private DataTable dtProductCategorys = new DataTable();
        private ResourceManager rm = new ResourceManager("Resources.SiteMap", System.Reflection.Assembly.Load("App_GlobalResources"));       

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                dtProductCategorys = DanhMucSanPhamProvider.GetDanhMucSanPhams(-1, "", "").Tables[0];

                try
                {
                    languageCode = Session[HangSo.LANGUAGE_CODE].ToString();
                    Session["NgonNgu"] = languageCode;
                    if (languageCode == "en-us")
                    {
                        IsVN = false;
                    }

                }
                catch (Exception ex)
                {
                }
                if (Session["UserName"] == null)
                {
                    Response.Redirect("EHTLogin.aspx");
                }
                Thread.CurrentThread.CurrentCulture = new CultureInfo(languageCode);
                LoadLanguage(Thread.CurrentThread.CurrentCulture);
                LoadModul();
            }
        }



        /// <summary>
        /// Ham load ngon ngu
        /// </summary>
        /// <param name="ci"></param>
        public void LoadLanguage(CultureInfo ci)
        {
            lbAddNew.Text = rm.GetString("lbAddNew", ci);
            lbDel.Text = rm.GetString("lbDel", ci);
            lbEdit.Text = rm.GetString("lbEdit", ci);
            lbHelp.Text = rm.GetString("lbHelp", ci);            
            lbTat.Text = rm.GetString("lbTat", ci);
            lbBat.Text = rm.GetString("lbBat", ci);
            lbModul.Text = rm.GetString("lbModul",ci);

        }
        /// <summary>
        /// Ham load Modul
        /// </summary>

        public void LoadModul()
        {
            //if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            if(IsVN==true)
            {
                rpModul.DataSource = ModulProvider.GetAllModul(true, "", false);
                rpModul.DataBind();
                plModulOther.Visible = false;
            }
            else
            {
                rpModulOther.DataSource = ModulProvider.GetAllModul(true, "", false);
                rpModulOther.DataBind();
                plModulVN.Visible = false;
            }
        }


        public void CheckAll(object sender, System.EventArgs e)
        {           
                CheckBox ckAll = (CheckBox)sender;
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpModul.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpModul.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
                                       
           
        }

        public void CheckAllOther(object sender, System.EventArgs e)
        {
            CheckBox ckAll = (CheckBox)sender;
                if (ckAll.Checked)
                {
                    foreach (RepeaterItem rp in rpModulOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = true;
                    }
                }
                else
                {
                    foreach (RepeaterItem rp in rpModulOther.Items)
                    {
                        CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                        ckMenu.Checked = false;
                    }
                }
        }

        protected void ImgEdit_Click(object sender, ImageClickEventArgs e)
        {
            Int32 ID = 0;
            Int32 Count = 0;
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpModul.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpModulOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        ID = Int32.Parse(lbDelID.Text);
                        Count++;
                    }
                }
            }
            if (Count > 1 || Count == 0)
            {
                String messa = "";
                if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
                    messa = HangSo.Eorr_Edit;
                else
                    messa = HangSo.Eorr_EditOther;
                ltrErorr.Text = HelpMethod.ErorrLogin(messa);
            }
            else
            {
                Response.Redirect("EHTModullAdd.aspx?ModulID=" + ID.ToString());
            }
        }

        protected void ImgDel_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();

            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpModul.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpModulOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                ModulProvider.UpdateChooseModul(Int32.Parse(arr[i].ToString()),false);
            LoadModul();
        }

        protected void ImgBat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
            {
                foreach (RepeaterItem rp in rpModul.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpModulOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                ModulProvider.ChangePublish(Int32.Parse(arr[i].ToString()), true);
            LoadModul();
        }

        protected void ImaTat_Click(object sender, ImageClickEventArgs e)
        {
            ArrayList arr = new ArrayList();
            //if (Session[HangSo.LANGUAGE_CODE].ToString() == "vi-VN")
           if(IsVN==true)
            {
                foreach (RepeaterItem rp in rpModul.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            else
            {
                foreach (RepeaterItem rp in rpModulOther.Items)
                {
                    CheckBox ckMenu = (CheckBox)rp.FindControl("ckMenu");
                    Label lbDelID = (Label)rp.FindControl("lbDelID");
                    if (ckMenu.Checked == true)
                    {
                        arr.Add(lbDelID.Text);
                    }
                }
            }
            for (int i = 0; i < arr.Count; i++)
                ModulProvider.ChangePublish(Int32.Parse(arr[i].ToString()), false);
            LoadModul();
        }

        protected void rpModul_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String cmdName = e.CommandName;
            Int32 ModulID = -1;
            try
            {
                ModulID = Int32.Parse(e.CommandArgument.ToString());
            }
            catch (Exception ex)
            {
            }

            if (cmdName == "change_number_of_order")
            {
                try
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                    ModulProvider.UpdateNumberOfOrder(ModulID, numberOfOrder);
                }
                catch (Exception ex)
                {

                }

                LoadModul();
            }
            else if (cmdName == "save_all")
            {
                foreach (RepeaterItem item in rpModul.Items)
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                    ModulID = Int32.Parse(((Label)item.FindControl("lbDelID")).Text.Trim());
                    ModulProvider.UpdateNumberOfOrder(ModulID, numberOfOrder);
                }

                LoadModul();
            }
        }

        protected void rpModul_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }

        protected void rpModulOther_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String cmdName = e.CommandName;
            Int32 ModulID = -1;
            try
            {
                ModulID = Int32.Parse(e.CommandArgument.ToString());
            }
            catch (Exception ex)
            {
            }

            if (cmdName == "change_number_of_order")
            {
                try
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)e.Item.FindControl("txtNumberOfOrder")).Text.Trim());
                    ModulProvider.UpdateNumberOfOrder(ModulID, numberOfOrder);
                }
                catch (Exception ex)
                {

                }

                LoadModul();
            }
            else if (cmdName == "save_all")
            {
                foreach (RepeaterItem item in rpModul.Items)
                {
                    Int32 numberOfOrder = Int32.Parse(((TextBox)item.FindControl("txtNumberOfOrder")).Text.Trim());
                    ModulID = Int32.Parse(((Label)item.FindControl("lbDelID")).Text.Trim());
                    ModulProvider.UpdateNumberOfOrder(ModulID, numberOfOrder);
                }

                LoadModul();
            }
        }

        protected void rpModulOther_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label lbStatus = (Label)e.Item.FindControl("lbStatus");
            Image ImagStatus = (Image)e.Item.FindControl("ImagStatus");
            try
            {
                if (lbStatus.Text == "True")
                    ImagStatus.ImageUrl = "~/images/tick.png";
                else
                    ImagStatus.ImageUrl = "~/images/Close.png";
            }
            catch
            {

            }
        }
    }
}